package com.smt.modules.job.service.impl;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 备份数据库
 *
 * @author zhangyi
 * @date 2018/6/21 16:03
 */
@Service("backUpDataBaseService")
@ConfigurationProperties(prefix = "backupsql")
public class BackUpDataBaseServiceImpl {

    private String hostip;
    private String mysqlusername;
    private String password;
    private String savepath;
    private String databasename;

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getHostip() {
        return hostip;
    }

    public void setHostip(String hostip) {
        this.hostip = hostip;
    }

    public String getMysqlusername() {
        return mysqlusername;
    }

    public void setMysqlusername(String mysqlusername) {
        this.mysqlusername = mysqlusername;
    }

    public String getSavepath() {
        return savepath;
    }

    public void setSavepath(String savepath) {
        this.savepath = savepath;
    }

    public String getDatabasename() {
        return databasename;
    }

    public void setDatabasename(String databasename) {
        this.databasename = databasename;
    }

    /**
     * 保留文件的数量
     */
    public static final Integer FILE_COUNT = 10;

    /**
     * 备份数据库
     *
     * @return
     */
    public boolean backup() throws IOException, InterruptedException {
        //返回标记状态
        boolean flag = false;

        //数据库的ip地址
        String hostIP = getHostip();
        //用户名
        String userName = getMysqlusername();
        //密码
        String password = getPassword();
        //保存路径
        String savePath = getSavepath();
        //保存文件名
        String fileName = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date(System.currentTimeMillis()))+".sql";
        //数据库名称
        String databaseName = getDatabasename();
        File file = new File(savePath+fileName);
        file.createNewFile();
        //保存的文件夹
        File saveFile = new File(savePath);
        //创建备份文件夹
        if(!saveFile.exists()){
            saveFile.mkdirs();
        }
        //删除老文件

        //执行linux上的命令
        String execStr = " mysqldump -h" + hostIP + " -u" + userName + " -p" + password + " --set-charset=UTF8 " + databaseName;
        //创建执行命令
        Process process = Runtime.getRuntime().exec(execStr);
        //读取命名输出流
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream(),"utf-8"));
        //将流写入文件
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),"utf-8"));
        //带读取字符
        String tempStr = "";
        while((tempStr = bufferedReader.readLine()) != null){
            bufferedWriter.write(tempStr);
            bufferedWriter.write( System.getProperty("line.separator"));
        }
        bufferedReader.close();
        bufferedWriter.close();
        //表示正常执行完成
        if(process.waitFor() == 0){
            flag = true;
        }
        return flag;
    }
}
